package maurice.controller;

import javax.swing.event.EventListenerList;
import maurice.controller.event.*;

public abstract class Controller extends Thread{
	private EventListenerList listeners;
	protected Direction direction;
	private boolean closed;

	public Controller() {
		listeners = new EventListenerList();
		this.direction = Direction.NONE;
		this.closed = false;
	}
	
	public void addControllerListener(ControllerListener listener) {
		listeners.add(ControllerListener.class, listener);
	}

	public void removeControllerListerner(ControllerListener listener) {
		listeners.remove(ControllerListener.class, listener);
	}

	public void fireMovementConfirmed() {
		ControllerListener[] controllerList = (ControllerListener[]) listeners
				.getListeners(ControllerListener.class);
		
		if (controllerList == null)
			return;

		for (ControllerListener listener : controllerList) {
			listener.movementConfirmed(new MovementConfirmedEvent(this, direction));
		}
	}
	
	public Direction getDirection(){
		return direction;
	}
	
	public void run(){
		while(!closed){};
	}
	
	public void close(){
		closed = true;
	}
}
